package com.gongpingjialoan.activity.car;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.utility.StepMonitor;
import com.handmark.pulltorefresh.library.smarttablayout.SmartTabLayout;
import com.handmark.pulltorefresh.library.utils.v4.FragmentPagerItem;
import com.handmark.pulltorefresh.library.utils.v4.FragmentPagerItemAdapter;
import com.handmark.pulltorefresh.library.utils.v4.FragmentPagerItems;

import static com.alibaba.sdk.android.feedback.impl.FeedbackAPI.mContext;

/**
 * Created by Administrator on 2015/10/19.
 */
public class NewRankFragment extends Fragment {

    private View mainV;

    private SmartTabLayout smartTabLayout;
    private ViewPager viewPager;
    private String[] actions = {"buy_billboard_hotModel", "buy_billboard_hotBrand", "buy_billboard_compactCar",
            "buy_billboard_suv", "buy_billboard_miniCar", "buy_billboard_smallCar",
            "buy_billboard_middleCar", "buy_billboard_middleSuperCar",
            "buy_billboard_superCar", "buy_billboard_luxuryCar", "buy_billboard_mpv"};
    private String[] types = {"model", "brand", "compact_car", "suv", "mini_car", "small_car", "middle_car", "middle_super_car", "super_car", "luxury_car", "mpv"};
    private String type;

    public int currentItem;
    public FragmentPagerItemAdapter adapter;
//    private boolean isUpdate = false;

    private int[] images = {R.drawable.model_selector, R.drawable.brand_selector, R.drawable.jcx_selector,
            R.drawable.suv_selector, R.drawable.wx_selector, R.drawable.xxc_selector,
            R.drawable.zj_selector, R.drawable.zgj_selector, R.drawable.pc_selector,
            R.drawable.hh_selector, R.drawable.mpv_selector};
    private String[] names = {"最热车型", "最热品牌", "紧凑型车", "SUV", "微型车", "小型车", "中级车", "中高级车", "跑车", "豪华车", "商务车"};

    @SuppressLint("InflateParams")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        mainV = inflater.inflate(R.layout.brand_model_layout, null);
        type = getActivity().getIntent().getStringExtra("typevalue");
        if (TextUtils.isEmpty(type)) {
            Bundle mArguments = getArguments();
            if(mArguments != null){
                type = getArguments().getString("type");
            }
        }
        if (TextUtils.isEmpty(type)) {
            type = types[0];
            currentItem = 0;
        }
        setRadioChecked();
        initView();
        return mainV;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden())
            StepMonitor.getInstance().addMonitor(new Monitor("enter", actions[currentItem], System.currentTimeMillis(), 1f));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            StepMonitor.getInstance().addMonitor(new Monitor("enter", actions[currentItem],  System.currentTimeMillis(), 1f));
        }
    }

    private void setRadioChecked() {
        int size = types.length;
        for (int i = 0; i < size; i++) {
            if (types[i].equals(type)) {
                currentItem = i;
                break;
            }
        }
    }


    public void refresh(String mtype) {
        if (TextUtils.isEmpty(mtype)) {
            mtype = types[0];
        }
        this.type = mtype;
        setRadioChecked();
        if (viewPager != null) {
            StepMonitor.getInstance().addMonitor(new Monitor("enter" , actions[currentItem] , System.currentTimeMillis() , 1f));
            viewPager.setCurrentItem(currentItem);
        }
    }


    private void initView() {
        PointRecordManage.getInstance().addPoint(mContext,"MarketBestSell");
        viewPager = (ViewPager) mainV.findViewById(R.id.view_pager);
        smartTabLayout = (SmartTabLayout) mainV.findViewById(R.id.viewpagertab);
        smartTabLayout.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.brand_model_item, container, false);
                ImageView imageView = (ImageView) view.findViewById(R.id.image);
                TextView textView = (TextView) view.findViewById(R.id.text);
                imageView.setImageResource(images[position]);
                textView.setText(names[position]);
                return view;
            }
        });

        FragmentPagerItems pages = new FragmentPagerItems(getActivity());
        int size = types.length;
        for (int i = 0; i < size; i++) {
            Bundle bundle = new Bundle();
            String title = types[i];
            bundle.putString("typevalue", title);
            bundle.putString("actions", actions[i]);
            if ("brand".equals(title)) {
                pages.add(FragmentPagerItem.of(title, BrandTopFragment.class, bundle));
            } else {
                pages.add(FragmentPagerItem.of(title, ModelTopFragment.class, bundle));
            }
        }

        adapter  = new FragmentPagerItemAdapter(
                getFragmentManager(), pages);
        viewPager.setAdapter(adapter);
        smartTabLayout.setViewPager(viewPager);

        viewPager.setCurrentItem(currentItem);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentItem = position;
                StepMonitor.getInstance().addMonitor(new Monitor("enter" , actions[position] , System.currentTimeMillis() , 1f));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


}
